package jt.classic.app.info;

import jt.classic.system.ISystem;
import jt.classic.system.SSOAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.purview.PurviewManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.Controller;
import org.limp.mine.DateTrimmer;
import org.limp.mine.HTMLTool;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;
import org.limp.mine.annex.AnnexInfo;

import java.sql.Connection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 内容管理视图action
 *
 * <p>继承ssoaction,不需要验证登录,可直接调用
 *
 * 会话用户初始化由ssoaction完成</p>
 *
 * @author Bizkit
 *
 */
public class InfoViewAction extends SSOAction {
    /**
    * 查看信息
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return
    * @throws Exception
    */
    public ActionForward viewinfo(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            InfoManager manager = new InfoManager(conn);

            AInfo info = manager.getAInfoById(id);

            if (info == null) {
                request.setAttribute("error", "该信息已被删除");
                forward = mapping.findForward("error");
            } else {
                IUser user = ISystem.getSessionUser(request);

                boolean canread = InfoTool.canread(info, user);

                if (!canread) {
                    request.setAttribute("error", "无权访问");
                    forward = mapping.findForward("error");
                } else {
                    InfoTraceManager tmanager = new InfoTraceManager(conn);
                    tmanager.insert(id, info.ititle(), "info", user,
                        request.getRemoteAddr());

                    manager.updateViewCount(id);

                    conn.commit();

                    request.setAttribute("info", info);

                    forward = mapping.findForward("viewinfo");
                }
            }
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * SSO 链接导向
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward navigate(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        String url = StringTool.checkString(request.getParameter("url"));

        if ((url.indexOf("'") == 0) &&
                (url.lastIndexOf("'") == (url.length() - 1))) {
            url = url.substring(url.indexOf("'") + 1, url.lastIndexOf("'"));
        }

        ActionForward forward = new ActionForward();
        forward.setPath(url);
        forward.setRedirect(true);

        return forward;
    }

    /**
     * 信息发布列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward listdirectory(ActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        IUser user = ISystem.getSessionUser(request);

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String key = StringTool.checkString(param.get("key"));

        String id = StringTool.checkString(param.get("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            InfoManager manager = new InfoManager(conn);

            AInfoDirectory adirec = null;

            if (!key.equals("")) {
                adirec = manager.getAInfoDirectoryByKey(key);
            } else {
                adirec = manager.getAInfoDirectoryById(id);
            }

            if (adirec == null) {
                request.setAttribute("error", "栏目已被删除");
                forward = mapping.findForward("error");
            } else {
                String pageNO = StringTool.checkString(param.get("pageNO"));

                String qname = StringTool.checkString(param.get("qname"));
                String qtype = StringTool.checkString(param.get("qtype"));
                String qdirec = StringTool.checkString(param.get("qdirec"));

                StringBuffer where = new StringBuffer(
                        " AND IF_DEL = 0 AND ID_ID = " + adirec.id());

                if (!qname.equals("")) {
                    where.append(" AND IF_TITLE LIKE '%" + qname + "%' ");
                }

                if (!qtype.equals("")) {
                    where.append(" AND IT_NAME = '" + qtype + "' ");
                }

                if (!qdirec.equals("")) {
                    where.append(" AND ID_NAME = '" + qdirec + "' ");
                }

                Map query = new HashMap();
                query.put("qname", qname);
                query.put("qtype", qtype);
                query.put("qdirec", qdirec);

                request.setAttribute("query", query);

                String flumeid = user.iallgroupflumeid(",");

                String lumeid = UserManager.iallgroupflumeid(flumeid);

                where.append(
                    " AND APP_INFO_ISSUE.IS_OBJ_TYPE = 'info' and APP_INFO_ISSUE.IS_AREAID in (" +
                    (lumeid.equals("") ? "'-1'" : lumeid) + ") AND IF_STATUS = 1 ");

                String sql1 = "SELECT COUNT(APP_INFO.IF_ID) FROM APP_INFO,APP_INFO_ISSUE WHERE APP_INFO.IF_ID = " +
                    "APP_INFO_ISSUE.IS_OBJ_ID " + where.toString() +
                    " GROUP BY APP_INFO.IF_ID";
                String sql2 = "SELECT APP_INFO.* FROM APP_INFO WHERE IF_ID IN (SELECT APP_INFO.IF_ID FROM APP_INFO," +
                    "APP_INFO_ISSUE WHERE APP_INFO.IF_ID = " +
                    "APP_INFO_ISSUE.IS_OBJ_ID " + where.toString() +
                    " GROUP BY APP_INFO.IF_ID) ORDER BY IF_TOP DESC, IF_DATE DESC";

                String url = request.getContextPath() +
                    "/app/Vinfo.do?method=listdirectory&key=" + key;

                request.setAttribute("direc", adirec);

                QueryJack plus = QueryJackFactory.getInstance()
                                                 .createQueryJack(false);

                // 设置
                plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
                plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
                plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
                plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
                plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
                plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

                // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
                List resultList = plus.work();

                // 获得跳转控制
                Controller controller = plus.getController("form1");

                // 设置调转类型
                controller.setType("input");

                // 显示标签
                Label label = plus.getLabel();

                // 设置显示标签类型
                label.setType("href");

                Object[] result = new Object[4];
                result[0] = resultList;
                result[1] = label;
                result[2] = controller;
                result[3] = pageNO;

                request.setAttribute("result", result);

                forward = mapping.findForward("listdirectory");
            }
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 静态加载幻灯片新闻信息
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward staticloadslideinfo(ActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Connection conn = null;

        String key = StringTool.checkString(request.getParameter("key"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            InfoManager manager = new InfoManager(conn);

            List list = manager.getSlideInfoByKey(key);

            if ((list != null) && !list.isEmpty()) {
                StringBuffer str = new StringBuffer();

                for (int i = 0; i < list.size(); i++) {
                    Map info = (Map) list.get(i);
                    str.append(info.get("IF_ID"));
                    str.append("#");
                    str.append(info.get("IF_TITLE"));
                    str.append("#");
                    str.append(info.get("IMG_URL"));

                    if (i < (list.size() - 1)) {
                        str.append(";");
                    }
                }

                request.setAttribute("result", str.toString());
            } else {
                request.setAttribute("result", "0");
            }
        } catch (Exception ex) {
            request.setAttribute("result", "-1");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 浏览目录列表(提供给外部系统服务使用,不带分页)
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward infolistjason(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        String id = StringTool.checkString(request.getParameter("id"));
        String key = StringTool.checkString(request.getParameter("key"));
        String size = StringTool.checkString(request.getParameter("size"), "10");

        int _size = 10;

        try {
            _size = Integer.parseInt(size);
        } catch (Exception ex) {
        }

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            InfoManager manager = new InfoManager(conn);
            AInfoDirectory direc = null;

            if (!id.equals("")) {
                direc = manager.getAInfoDirectoryById(id);
            } else if (!key.equals("")) {
                direc = manager.getAInfoDirectoryByKey(key);
            }

            if (direc == null) {
                request.setAttribute("result",
                    "{success: false,\"totalCount\":\"-2\"}");
            } else {
                IUser user = ISystem.getSessionUser(request);
                PurviewManager pmanager = new PurviewManager(conn);
                boolean canread = pmanager.purview4read(direc.id(),
                        "directory", user);

                if (!canread) {
                    request.setAttribute("result",
                        "{success: false,\"totalCount\":\"-3\"}");
                } else {
                    request.setAttribute("direc", direc);

                    List list = manager.getIssueAInfoInDirectory(user,
                            direc.ikey(), _size);

                    if ((list != null) && !list.isEmpty()) {
                        for (int i = 0; i < list.size(); i++) {
                            Map info = (Map) list.get(i);
                            String infoid = (String) info.get("IF_ID");
                            String infotitle = (String) info.get("IF_TITLE");
                            String infodate = DateTrimmer.getYMD_LBL((String) info.get(
                                        "IF_DATE"));
                            String infouser = (String) info.get("IF_USERNAME");

                            InfoJsonBean bean = new InfoJsonBean();
                            bean.setId(infoid);
                            bean.setTitle(infotitle);
                            bean.setDate(infodate);
                            bean.setUserName(infouser);
                            bean.setContent((String) info.get("IF_CONTENT"));
                            bean.setDirecName((String) info.get("ID_NAME"));
                            bean.setInvolvement(Integer.parseInt(
                                    (String) info.get("IF_IVCOUNT")));
                            bean.setReply(Integer.parseInt(
                                    (String) info.get("IF_RECOUNT")));
                            bean.setView(Integer.parseInt(
                                    (String) info.get("IF_VECOUNT")));
                            bean.setTop(Integer.parseInt(
                                    (String) info.get("IF_TOP")));
                            bean.setCanReplay(Integer.parseInt(
                                    (String) info.get("IF_RECEIPT")));
                            bean.setSummary((String) info.get("IF_SUMMARY"));
                            bean.setSource((String) info.get("IF_PROVIDE"));

                            list.set(i, bean);
                        }
                    }

                    JSONArray jsonArray = JSONArray.fromObject(list);

                    String jsonString = "{\"success\": \"true\",\"name\": \"" +
                        direc.iname() + "\",\"totalCount\":\"" + list.size() +
                        "\",\"infoList\":";
                    jsonString += jsonArray.toString();
                    jsonString += "}";

                    request.setAttribute("result", jsonString);
                }
            }
        } catch (Exception ex) {
            request.setAttribute("result",
                "{success: false,\"totalCount\":\"-1\"}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("jsonresult");
    }

    /**
     * 浏览目录列表(提供给外部系统服务使用,带分页功能)
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward infolistPjason(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String id = StringTool.checkString(param.get("id"));
        String key = StringTool.checkString(param.get("key"));
        String sort = StringTool.checkString(param.get("sort"));
        String dir = StringTool.checkString(param.get("dir"));

        String name = StringTool.checkString(param.get("name"));

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String totalCount = StringTool.checkString(param.get("totalCount"));

        String start = StringTool.checkString(param.get("start"), "0");

        String size = StringTool.checkString(param.get("limit"), "20");

        if (pageNO.equals("")) {
            pageNO = String.valueOf((Integer.parseInt(start) / Integer.parseInt(
                        size)) + 1);
        }

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            InfoManager manager = new InfoManager(conn);
            AInfoDirectory direc = null;

            String jsonString = "{success: true,\"totalCount\":\"0\",purview: '-1',totalPage:'0',pageNO:'0',\"infoList\":[]}";

            if (!id.equals("")) {
                direc = manager.getAInfoDirectoryById(id);
            } else if (!key.equals("")) {
                direc = manager.getAInfoDirectoryByKey(key);
            }

            if (direc != null) {
                IUser user = ISystem.getSessionUser(request);
                PurviewManager pmanager = new PurviewManager(conn);
                boolean canread = pmanager.purview4read(direc.id(),
                        "directory", user);

                if (canread) {
                    request.setAttribute("direc", direc);

                    StringBuffer where = new StringBuffer(" AND ID_ID = " +
                            direc.id());

                    String lumeid = user.iallgroupflumeid(",");

                    lumeid = UserManager.iallgroupflumeid(lumeid);

                    where.append(
                        " AND APP_INFO_ISSUE.IS_OBJ_TYPE = 'info' and APP_INFO_ISSUE.IS_AREAID in (" +
                        (lumeid.equals("") ? "'-1'" : lumeid) + ")");

                    if (!name.equals("")) {
                        where.append(" AND APP_INFO.IF_TITLE LIKE '%" + name +
                            "%'");
                    }

                    String sql1 = "SELECT COUNT(*) AS TOTAL FROM (SELECT COUNT(APP_INFO.IF_ID) AS TOTAL FROM APP_INFO," +
                        "APP_INFO_ISSUE WHERE APP_INFO.IF_ID = " +
                        "APP_INFO_ISSUE.IS_OBJ_ID AND IF_DEL = 0 AND IF_STATUS = 1 " +
                        where.toString() + " GROUP BY APP_INFO.IF_ID) A";

                    String sql2 = "SELECT APP_INFO.IF_ID FROM APP_INFO," +
                        "APP_INFO_ISSUE WHERE APP_INFO.IF_ID = " +
                        "APP_INFO_ISSUE.IS_OBJ_ID AND IF_DEL = 0 AND IF_STATUS = 1 " +
                        where.toString() + " GROUP BY APP_INFO.IF_ID ";

                    if (!sort.equals("") && !dir.equals("")) {
                        if (sort.equals("date")) {
                            sql2 += (" ORDER BY APP_INFO.IF_DATE " + dir);
                        } else if (sort.equals("title")) {
                            sql2 += (" ORDER BY APP_INFO.IF_TITLE " + dir);
                        }
                    } else {
                        sql2 += " ORDER BY IF_TOP DESC, IF_DATE DESC";
                    }

                    QueryJack plus = QueryJackFactory.getInstance()
                                                     .createQueryJack(false);

                    //设置
                    plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
                    plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
                    plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
                    plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, "");
                    plus.setParameter(QueryJackFactory.PAGINATION_COUNT, size);
                    plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

                    // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
                    List list = plus.work();

                    if ((list != null) && !list.isEmpty()) {
                        for (int i = 0; i < list.size(); i++) {
                            Map temp = (Map) list.get(i);
                            String infoid = (String) temp.get("IF_ID");

                            AInfo info = manager.getAInfoById(infoid);

                            List alist = info.iannexs();

                            if ((alist != null) && !alist.isEmpty()) {
                                for (int j = 0; j < alist.size(); j++) {
                                    String[] str = (String[]) alist.get(j);
                                    Map atemp = new HashMap();

                                    atemp.put("id", str[0]);
                                    atemp.put("name", str[2]);
                                    atemp.put("type", str[3]);
                                    atemp.put("link", str[4]);

                                    alist.set(j, atemp);
                                }
                            }

                            InfoJsonBean bean = new InfoJsonBean();
                            bean.setIdx((String) temp.get(
                                    QueryJackFactory.PAGINATION_NUMBER));
                            bean.setId(info.id());
                            bean.setTitle(info.ititle());
                            bean.setDate(DateTrimmer.getYMD_LBL(info.idate()));
                            bean.setUserName(info.iuser().iname());
                            bean.setContent(info.icontent().toString());
                            bean.setDirecName(info.idirectory().iname());
                            bean.setInvolvement(info.involvementCount());
                            bean.setReply(info.ireplyCount());
                            bean.setView(info.iviewCount());
                            bean.setTop(info.itop());
                            bean.setCanReplay(info.ireceipt() ? 1 : 0);
                            bean.setSummary(info.isummary());
                            bean.setSource(info.iprovide());
                            bean.setAnnexs(alist);

                            list.set(i, bean);
                        }
                    }

                    //显示标签
                    Label label = plus.getLabel();

                    JSONArray jsonArray = JSONArray.fromObject(list);

                    jsonString = "{success: true,purview: '0',totalPage:" +
                        label.totalfan() + ",pageNO:" + pageNO +
                        ",\"totalCount\":" + label.totaldata() +
                        ",\"infoList\":";
                    jsonString += jsonArray.toString();
                    jsonString += "}";
                }
            } else {
                jsonString = "{success: true,\"totalCount\":0,purview: '-2',totalPage:0,pageNO:0,\"infoList\":[]}";
            }

            request.setAttribute("result", jsonString);
        } catch (Exception ex) {
            request.setAttribute("result",
                "{success: false,\"totalCount\":\"-1\"}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("jsonresult");
    }

    /**
     * 浏览目录列表-查询功能(提供给外部系统服务使用,带分页功能)
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward infoquerylistPjason(ActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Connection conn = null;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String sort = StringTool.checkString(param.get("sort"));
        String dir = StringTool.checkString(param.get("dir"));

        String name = StringTool.checkString(param.get("name"));

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String totalCount = StringTool.checkString(param.get("totalCount"));

        String start = StringTool.checkString(param.get("start"), "0");

        String size = StringTool.checkString(param.get("limit"), "20");

        pageNO = String.valueOf((Integer.parseInt(start) / Integer.parseInt(
                    size)) + 1);

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            InfoManager manager = new InfoManager(conn);

            StringBuffer where = new StringBuffer();

            String lumeid = user.iallgroupflumeid(",");

            lumeid = UserManager.iallgroupflumeid(lumeid);

            where.append(
                " AND APP_INFO_ISSUE.IS_OBJ_TYPE = 'info' and APP_INFO_ISSUE.IS_AREAID in (" +
                (lumeid.equals("") ? "'-1'" : lumeid) + ")");

            if (!name.equals("")) {
                where.append(" AND APP_INFO.IF_TITLE LIKE '%" + name + "%'");
            }

            String sql1 = "SELECT COUNT(APP_INFO.IF_ID) AS TOTAL FROM APP_INFO," +
                "APP_INFO_ISSUE WHERE APP_INFO.IF_ID = " +
                "APP_INFO_ISSUE.IS_OBJ_ID AND IF_DEL = 0 AND IF_STATUS = 1 " +
                where.toString() + " GROUP BY APP_INFO.IF_ID";

            String sql2 = "SELECT APP_INFO.IF_ID FROM APP_INFO," +
                "APP_INFO_ISSUE WHERE APP_INFO.IF_ID = " +
                "APP_INFO_ISSUE.IS_OBJ_ID AND IF_DEL = 0 AND IF_STATUS = 1 " +
                where.toString() + " GROUP BY APP_INFO.IF_ID ";

            if (!sort.equals("") && !dir.equals("")) {
                if (sort.equals("date")) {
                    sql2 += (" ORDER BY APP_INFO.IF_DATE " + dir);
                } else if (sort.equals("title")) {
                    sql2 += (" ORDER BY APP_INFO.IF_TITLE " + dir);
                }
            } else {
                sql2 += " ORDER BY IF_TOP DESC, IF_DATE DESC";
            }

            QueryJack plus = QueryJackFactory.getInstance()
                                             .createQueryJack(false);

            //设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, "");
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, size);
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List list = plus.work();

            if ((list != null) && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    Map temp = (Map) list.get(i);
                    String infoid = (String) temp.get("IF_ID");

                    AInfo info = manager.getAInfoById(infoid);

                    List alist = info.iannexs();

                    if ((alist != null) && !alist.isEmpty()) {
                        for (int j = 0; j < alist.size(); j++) {
                            String[] str = (String[]) alist.get(j);
                            Map atemp = new HashMap();

                            atemp.put("id", str[0]);
                            atemp.put("name", str[2]);
                            atemp.put("type", str[3]);
                            atemp.put("link", str[4]);

                            alist.set(j, atemp);
                        }
                    }

                    InfoJsonBean bean = new InfoJsonBean();
                    bean.setIdx((String) temp.get(
                            QueryJackFactory.PAGINATION_NUMBER));
                    bean.setId(info.id());
                    bean.setTitle(info.ititle());
                    bean.setDate(DateTrimmer.getYMD_LBL(info.idate()));
                    bean.setUserName(info.iuser().iname());
                    bean.setContent(info.icontent().toString());
                    bean.setDirecName(info.idirectory().iname());
                    bean.setInvolvement(info.involvementCount());
                    bean.setReply(info.ireplyCount());
                    bean.setView(info.iviewCount());
                    bean.setTop(info.itop());
                    bean.setCanReplay(info.ireceipt() ? 1 : 0);
                    bean.setSummary(info.isummary());
                    bean.setSource(info.iprovide());
                    bean.setAnnexs(alist);

                    list.set(i, bean);
                }
            }

            //显示标签
            Label label = plus.getLabel();

            JSONArray jsonArray = JSONArray.fromObject(list);

            String jsonString = "{success: true,purview: '0',totalPage:'" +
                label.totalfan() + "',pageNO:'0',\"totalCount\":\"" +
                label.totaldata() + "\",\"infoList\":";
            jsonString += jsonArray.toString();
            jsonString += "}";

            request.setAttribute("result", jsonString);
        } catch (Exception ex) {
            request.setAttribute("result",
                "{success: false,\"totalCount\":\"-1\"}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("jsonresult");
    }

    /**
     * 浏览信息列表(提供给外部系统服务使用)
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward infojason(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        String json = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            InfoManager manager = new InfoManager(conn);

            AInfo info = manager.getAInfoById(id);

            if (info == null) {
                json = "{success:false, errorCode: '-1'}";
            } else {
                int status = Integer.parseInt((String) info.getResource()
                                                           .get("IF_STATUS"));

                if (status < 1) {
                    json = "{success:false, errorCode: '1'}";
                } else {
                    IUser user = ISystem.getSessionUser(request);

                    boolean canread = ((status == 1) &&
                        InfoTool.canread(info, user));

                    if (!canread) {
                        json = "{success:false, errorCode: '0'}";
                    } else {
                        InfoTraceManager tmanager = new InfoTraceManager(conn);
                        tmanager.insert(id, info.ititle(), "info", user,
                            request.getRemoteAddr());

                        manager.updateViewCount(id);
                        conn.commit();

                        List alist = info.iannexs();

                        if ((alist != null) && !alist.isEmpty()) {
                            for (int i = 0; i < alist.size(); i++) {
                                String[] str = (String[]) alist.get(i);
                                Map temp = new HashMap();

                                temp.put("id", str[0]);
                                temp.put("name", str[2]);
                                temp.put("type", str[3]);
                                temp.put("link", str[4]);

                                alist.set(i, temp);
                            }
                        }

                        JSONArray jsonArray = JSONArray.fromObject(alist);

                        json = "{success:true, id : '" + info.id() +
                            "', title: '" +
                            HTMLTool.htmlEscape(info.ititle(), false, false,
                                true, true) + "', content : '" +
                            HTMLTool.htmlEscape((String) info.icontent(),
                                false, false, true, true) + "', " + "font : '" +
                            info.ititlefont() + "', size :'" +
                            info.ititlesize() + "',color:'" +
                            info.ititlecolor() + "',userid : '" +
                            info.iuser().id() + "', username : '" +
                            info.iuser().iname() + "', groupname : '" +
                            ((info.iuser().igroup() != null)
                            ? info.iuser().igroup().iflumelabel("/") : "") +
                            "'," + "direcid : '" + info.idirectory().id() +
                            "', direcname: '" + info.idirectory().iname() +
                            "',direcdesc : '" + info.idirectory().imemo() +
                            "',direckey:'" + info.idirectory().ikey() +
                            "',date : '" +
                            DateTrimmer.getYMD_LBL(info.idate()) +
                            "',receipt:'" + info.ireceipt() + "',top:'" +
                            info.itop() + "',summary:'" +
                            HTMLTool.htmlEscape(info.isummary(), false, false,
                                true, true) + "',provide:'" + info.iprovide() +
                            "',reply:'" + info.ireplyCount() +
                            "',involvement:'" + info.involvementCount() +
                            "',trace:'" + (info.iviewCount() + 1) +
                            "',annexs:" + jsonArray.toString() + "}";
                    }
                }
            }
        } catch (Exception ex) {
            json = "{success:false, errorCode: '-2'}";
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        request.setAttribute("result", json);

        return mapping.findForward("jsonresult");
    }
}
